home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n1.arc / L4.C < prev    next >
Text File  |  1990-06-08  |  877b  |  39 lines

  1. /*
  2.  * *** Listing 4 ***
  3.  *
  4.  * Program to calculate the 16-bit checksum of the stream of bytes
  5.  * from the specified file. Obtains the bytes one at a time via
  6.  * getc(), allowing C to perform data buffering.
  7.  */
  8. #include <stdio.h>
  9.  
  10. main(int argc, char *argv[]) {
  11.    FILE *CheckFile;
  12.    int Byte;
  13.    unsigned int Checksum;
  14.  
  15.    if ( argc != 2 ) {
  16.       printf("usage: checksum filename\n");
  17.       exit(1);
  18.    }
  19.  
  20.    if ( (CheckFile = fopen(argv[1], "rb")) == NULL ) {
  21.       printf("Can't open file: %s\n", argv[1]);
  22.       exit(1);
  23.    }
  24.  
  25.    /* Initialize the checksum accumulator */
  26.    Checksum = 0;
  27.  
  28.    /* Add each byte in turn into the checksum accumulator */
  29.    while ( (Byte = getc(CheckFile)) != EOF ) {
  30.       Checksum += (unsigned int) Byte;
  31.    }
  32.  
  33.    /* Report the result */
  34.    printf("The checksum is: %u\n", Checksum);
  35.  
  36.    exit(0);
  37. }
  38.  
  39.